package com.yuehuanghun.admin.core.config;

import java.util.Map;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.yuehuanghun.admin.core.config.guava.GuavaCacheManager;
import com.yuehuanghun.admin.core.config.guava.GuavaCacheManager.CacheConfig;

import lombok.Setter;

/**
 * 用于未设置RedisCacheManager时作为CacheManager的默认实现
 * @see com.yuehuanghun.framework.service.DefaultRedisConfiguration
 * @author yadmin
 *
 */
@ConfigurationProperties(prefix = "spring.cache", ignoreUnknownFields = true)
@Configuration
@ConditionalOnMissingBean(CacheManager.class)
public class GuavaCacheManagerConfiguration {
	@Setter
	private Map<String, Long> expires;
	
	@Bean
	public CacheManager guavaCacheManager() {
		GuavaCacheManager cacheManager = new GuavaCacheManager();
		if(expires != null) {
			expires.forEach((cacheName, ttlSeconds) -> {
				cacheManager.addCacheConfig(new CacheConfig(cacheName, ttlSeconds));
			});
		}
		
		return cacheManager;
	}
}
